package artid.com.arttogo.wxapi;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import artid.com.arttogo.R;
import artid.com.arttogo.activity.ChoicePayActivity;
import artid.com.arttogo.activity.PaySuccessActivity;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.pay.Constants;
import artid.com.arttogo.utils.ToastUtil;

/**
 *
 *
 * 类名称   ：WXPayEntryActivity
 * 类描述   ：微信支付回调类
 * 创建人   ：耿卫斌
 *
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private IWXAPI api;
	private TextView mTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payresults);
		api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
		mTextView=(TextView) findViewById(R.id.WXPayEntry_paytype);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}


	@Override
	public void onResp(BaseResp resp) {
		Log.d("log", "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Log.i("支付结果", "支付结果"+resp.errStr+"code"+resp.errCode);
			switch (resp.errCode) {
				case 0:
                    ToastUtil.toast(this,"支付成功！");
//                    ArtToGoApp.getInstance().addMallActivity(ChoicePayActivity);
                    Intent intent = new Intent(this,PaySuccessActivity.class);
                    intent.putExtra("id", ChoicePayActivity.id);
                    startActivity(intent);
                    ChoicePayActivity.isNeedClose = true;
                    finish();
					break;
				case -1:
					Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
					mTextView.setText("支付失败");
                    finish();
					break;
				case -2:
					Toast.makeText(this, "取消支付", Toast.LENGTH_SHORT).show();
					mTextView.setText("取消支付");
                    finish();
					break;
				default:
					Toast.makeText(this, "支付错误", Toast.LENGTH_SHORT).show();
					mTextView.setText("支付错误");
                    finish();
					break;
			}
		}
	}
}